<?php

namespace app\common\model;

use app\beautiful\model\PrinterModel;
use app\common\model\account\ShopAccountModel;
use think\facade\Db;


class ProduceDepartModel extends CommonModel{

    protected  $name = 'beautiful_produce_depart';

    protected  $pk   = 'id';


    public static function info($where){
        $info = Db::name('beautiful_produce_depart')->alias('a')
            ->join('beautiful_produce_type b','a.type_id=b.id')
            ->leftJoin('account c','a.create_user_id=c.account_id')
            ->leftJoin('account d','a.edit_user_id=d.account_id')
            ->where(['a.is_delete'=>0])
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(!empty($where['id']),function ($query) use ($where){
                $query->where(['a.id'=>$where['id']]);
            })
            ->when(!empty($where['type_id']),function ($query) use ($where){
                $query->where(['a.type_id'=>$where['type_id']]);
            })
            ->when(isset($where['is_effect']),function ($query) use ($where){
                $query->where(['a.is_effect'=>$where['is_effect']]);
            })
            ->when(!empty($where['name']),function ($query) use ($where){
                $query->where('a.name','like','%'.$where['name'].'%');
            })
            ->field("a.*,b.name as type_name,c.username as create_name,d.username as edit_name")
            ->find();
        $info['print_list']=[];
        if(!empty($info['print_ids'])){
            $print_id=explode(',',$info['print_ids']);
            if(!empty($print_id)){
                $print=PrinterModel::where('printer_id','in',$print_id)->select()->toArray();
                $info['print_list']=$print;
            }
        }
        return $info;
    }
    public static function list($where,$order,$page=1,$limit=10){
        $query = Db::name('beautiful_produce_depart')->alias('a')
            ->join('beautiful_produce_type b','a.type_id=b.id')
            ->leftJoin('account c','a.create_user_id=c.account_id')
            ->leftJoin('account d','a.edit_user_id=d.account_id')
            ->where(['a.is_delete'=>0])
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['a.shop_id'=>$where['shop_id']]);
            })
            ->when(!empty($where['origin_type']),function ($query) use ($where){
                $query->where(['a.origin_type'=>$where['origin_type']]);
            })
            ->when(!empty($where['type_id']),function ($query) use ($where){
                $query->where(['a.type_id'=>$where['type_id']]);
            })
            ->when(isset($where['is_effect']),function ($query) use ($where){
                $query->where(['a.is_effect'=>$where['is_effect']]);
            })
            ->when(!empty($where['name']),function ($query) use ($where){
                $query->where('a.name','like','%'.$where['name'].'%');
            })
            ->field("a.*,b.name as type_name,c.username as create_name,d.username as edit_name");
        $count=$query->count();
        $list=$query->order($order)
            ->limit($limit)->page($page)
            ->select()
            ->toArray();
        if(!empty($list)){
            foreach ($list as &$v){
                if(!empty($v['print_ids'])){
                    $print_id=explode(',',$v['print_ids']);
                    if(!empty($print_id)){
                        $print=PrinterModel::where('printer_id','in',$print_id)->select()->toArray();
                        $v['print_list']=$print;
                    }
                }
                $v['goods_ids'] = ProduceModel::where(['is_delete'=>0,'depart_id'=>$v['id']])->column('goods_id');
            }
        }
        return compact('count','list');
    }
    public static function up($data,$where){
        $data['edit_time']=date('Y-m-d H:i:s');
        $res=self::update($data,$where);
        return $res;
    }
    public static function getById($where,$field='*')
    {
        $list = Db::name('beautiful_produce_depart')->where($where)->field($field)->find();

        return $list;
    }
}

